//
//  XLHosterReusableView.m
//  XiangLove
//
//  Created by feng song on 2018/1/25.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLHosterReusableView.h"

@implementation XLHosterReusableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self interView];
    }
    return self;
}

-(void)setOrderNum:(NSInteger)orderNum
{
    _orderNum = orderNum;
    [self interView];
}
-(void)interView
{
    
    UIImageView * headImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, XQScreenW, 200)];
    //[headImg setImageWithURL:[NSURL URLWithString:@""] placeholder:[UIImage imageNamed:@""]];
    headImg.clipsToBounds = YES;
    headImg.contentMode = UIViewContentModeScaleAspectFill;
    headImg.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [headImg setContentScaleFactor:[[UIScreen mainScreen] scale]];
    headImg.image = [UIImage imageNamed:@"home_bg_banner__n"];
    [self addSubview:headImg];
    
    _grabSingleImg = [[UIImageView alloc] init];
    _grabSingleImg.image = [UIImage imageNamed:@"home_but_qiang_n"];
    [headImg addSubview:_grabSingleImg];
    [_grabSingleImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headImg.mas_top).offset(62);
        make.centerX.equalTo(headImg);
        make.size.mas_equalTo(CGSizeMake(72, 72));
    }];
    
    _redPoint  = [[UILabel alloc] init];
    _redPoint.textColor = kWhiteColor;
    _redPoint.font = kSetFont(10);
    _redPoint.layer.masksToBounds = YES;
    _redPoint.layer.cornerRadius  = 8;
    _redPoint.backgroundColor = RGBA(248, 75, 90, 1);
    _redPoint.textAlignment = NSTextAlignmentCenter;
    [_grabSingleImg addSubview:_redPoint];
    [_redPoint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 16));
        make.center.equalTo(_grabSingleImg).centerOffset(CGPointMake(28, -15));
    }];
    [self updateCellOnline];
    
    UILabel * grabSingLab = [[UILabel alloc] init];
    grabSingLab.text = @"开始抢单";
    grabSingLab.textColor = kWhiteColor;
    grabSingLab.font = KSetBoldFont(15.0);
    grabSingLab.textAlignment =NSTextAlignmentCenter;
    [headImg addSubview:grabSingLab];
    [grabSingLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_grabSingleImg.mas_bottom).offset(7);
        make.centerX.equalTo(headImg);
        make.size.mas_equalTo(CGSizeMake(65, 21));
    }];
    
}

- (void)updateCellOnline
{
    NSInteger readCount = _orderNum;
    if ( readCount <= 9 &&  readCount > 0)
    {
        _redPoint.hidden = NO;
        [_redPoint mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(20, 16));
            make.center.equalTo(_grabSingleImg).centerOffset(CGPointMake(28, -15));
        }];
        
    }else if (readCount >= 10) {
        _redPoint.hidden = NO;
        [_redPoint mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(30, 20));
            make.center.equalTo(_grabSingleImg).centerOffset(CGPointMake(28, -15));
        }];
        
    }else {
        _redPoint.hidden = YES;
        [_redPoint mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(0, 0));
            make.center.equalTo(_grabSingleImg).centerOffset(CGPointMake(28, -15));
        }];
        
    }
    
    [_redPoint setText:readCount > 99 ? @"99+" : [NSString stringWithFormat:@"%ld",readCount]];
    
}

@end
